package com.xisoft.base.entity;


import lombok.Data;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Data
public abstract class BaseVO implements IBaseVO, Serializable, Cloneable
{

    public static long serialVersionUID = 1L;

    public Map< String,String > childEntity = new LinkedHashMap();

    public Map< String,List > childInfo = new LinkedHashMap();

    @Override public String getTableName( )
    {
        return "";
    }

    @Override public String getPrimaryKey( )
    { return ""; }

    /************************************************************* <br>
     *
     *说明: 通用的set<br>
     *
     * @Description   <br>
     * @Date Created on 2022/7/19  10:30  <br>
     * @param propertyName
     * @param propertyValue <br>
     * @return void  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public void set(String propertyName, Object propertyValue)
    {
        try
        {
            Field field = this.getClass().getDeclaredField(propertyName);
            //setAccessible这行代码把对象上的propertyName字段设置为public访问属性.
            field.setAccessible(true);
            // 设置该对象的propertyValue属性
            field.set(this, propertyValue);
        }
        catch (Exception e) { throw new RuntimeException(e); }
    }

    /************************************************************* <br>
     *
     *说明:  通用的get<br>
     *
     * @Description   <br>
     * @Date Created on 2022/7/19  10:30  <br>
     * @param propertyName <br>
     * @return java.lang.Object  <br>
     * @Author LIBENCHENG
     * @version 1.0 <br>
     ************************************************************* <br>
     */
    public Object get(String propertyName)
    {
        try
        {
            // 获取该对象的Class,获取所有的属性数组
            BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors)
            {
                String key = property.getName();
                // 过滤class属性
                if (!(key.equals("class") || key.equals("Class")))
                {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    //获取属性对应的 value
                    if (key.equals(propertyName))
                    { return getter.invoke(this); }
                }
            }
        }
        catch (Exception e) { throw new RuntimeException(e); }

        return null;
    }

    // @Override public Object getAttributeValue(String name) throws IntrospectionException, InvocationTargetException,
    //         IllegalAccessException
    // { return this.get(name); }
    //
    // @Override public void setAttributeValue(String name, Object value) throws NoSuchFieldException,
    //         IllegalAccessException
    // { this.set(name, value); }

    @Override public Object clone( )
    {
        try
        {
            // 将对象写到流⾥
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            // 从流⾥读出来
            new ObjectOutputStream(bo).writeObject(this);
            ObjectInputStream oi = new ObjectInputStream(new ByteArrayInputStream(bo.toByteArray()));
            return (oi.readObject());
        }
        catch (Exception e)
        {
            return this;
        }
    }

    @Override public String toString( )
    { return super.toString(); }
}
